import { location } from "../utils/getLocation";
import settings from "../utils/settings";
import { MessageID, ServerTick, copyToClipboard } from "../utils/utils";

let RareDropItems = {
    Ignore : ["potato","carrot","cropie","squash","compost","tasty cheese","enchanted bone","enchanted ender pearl"],
    Share : {"phoenix": {"copy": false,"ac": false,"pc": false,"gc": true},"warden heart": {"copy": false,"ac": false,"pc": false,"gc": true},"overflux capacitor": {"copy": false,"ac": false,"pc": false,"gc": true},"judgement core": {"copy": false,"ac": false,"pc": false,"gc": true}}
};

function SaveFile() {
    FileLib.write("./config/ChatTriggers/modules/BabyzombieAddons/config/RareDrop.json",JSON.stringify(RareDropItems,null,4),true);
}

if(FileLib.exists("./config/ChatTriggers/modules/BabyzombieAddons/config/RareDrop.json")) try {
    const Files = JSON.parse(FileLib.read("./config/ChatTriggers/modules/BabyzombieAddons/config/RareDrop.json"));
    for (let key in RareDropItems) {
        if(Files[key]) RareDropItems[key] = Files[key];
    }
} catch (error) {}

export function RareDrop(str=[""]) {
    switch (str[0]) {
        case "i":
        case "ig":
        case "ign":
        case "igno":
        case "ignor":
        case "ignore":
            switch (str[1]) {
                case "l":
                case "list":
                    let IgnoreListMessage = new Message(
                        "§6§l[BZA] §a稀有掉落屏蔽列表§7:","\n",
                        new TextComponent(`§1§m                                                                  `).setHover("show_text","§6这是一条分割线") , ' \n' , 
                    ).setChatLineId(MessageID.RareDropSetting);
                    RareDropItems.Ignore.forEach(e => {
                        IgnoreListMessage.addTextComponent(new TextComponent("§b" + e).setHover("show_text","§e点击将 " + e + " 从稀有掉落屏蔽列表中移除").setClick("run_command","/babyzombieaddons raredrop ignore listremove " + e));
                        IgnoreListMessage.addTextComponent("\n");
                    })
                    IgnoreListMessage.addTextComponent(new TextComponent(`§1§m                                                                  `).setHover("show_text","§6这是一条分割线"));
                    IgnoreListMessage.chat();
                    break;
                case "a":
                case "add":
                    RareDropItems.Ignore.push(str.slice(2).join(' ').toLowerCase());
                    ChatLib.chat("§6§l[BZA] §a已将 §b" + str.slice(2).join(' ').toLowerCase() + " §a添加至稀有掉落屏蔽列表");
                    SaveFile();
                    break;
                case "d":
                case "del":
                case "delete":
                case "r":
                case "rem":
                case "remove":
                    ChatLib.chat((RareDropItems.Ignore.filter(e => e == str.slice(2).join(' ').toLowerCase()).length ? "§6§l[BZA] §a已从稀有掉落屏蔽列表中删除§7: §b" + str.slice(2).join(' ').toLowerCase() : "§6§l[BZA] §e稀有掉落屏蔽列表中未找到§7: §b" + str.slice(2).join(' ').toLowerCase()));
                    RareDropItems.Ignore = RareDropItems.Ignore.filter(e => e != str.slice(2).join(' ').toLowerCase());
                    SaveFile();
                    break;
                case "listremove":
                    RareDropItems.Ignore = RareDropItems.Ignore.filter(e => e != str.slice(2).join(' ').toLowerCase());
                    SaveFile();
                    ChatLib.command("babyzombieaddons raredrop ignore list",true);
                    break;
                default:
                    new Message(
                        new TextComponent("§4←§r").setClick("run_command",`/${settings.modulecmd} raredrop`).setHover("show_text","§b返回上一页"),
                        new TextComponent("                       §b稀有掉落屏蔽 功能帮助\n"),
                        new TextComponent("§1§m                                                                  ").setHover("show_text","§6这是一条分割线") , ' \n' , 
                        new TextComponent(`§a/${settings.modulecmd} raredrop ignore add <物品名> §7- §a添加物品`).setClick("suggest_command",`/${settings.modulecmd} raredrop ignore add `).setHover("show_text","§a物品名不区分大小写\n§e点击快速输入指令"),"\n",
                        new TextComponent(`§a/${settings.modulecmd} raredrop ignore del <物品名> §7- §a移除物品`).setClick("suggest_command",`/${settings.modulecmd} raredrop ignore del `).setHover("show_text","§a物品名不区分大小写\n§e点击快速输入指令"),"\n",
                        new TextComponent(`§a/${settings.modulecmd} raredrop ignore list        §7- §a查看列表`).setClick("run_command",`/${settings.modulecmd} raredrop ignore list`).setHover("show_text","§e点击查看列表"),"\n",
                        new TextComponent("§a可检测怪物的稀有掉落,slayer boss的掉落,农业套装效果,花园虫子掉落"),"\n",
                        new TextComponent("§1§m                                                                  ").setHover("show_text","§6这是一条分割线")
                    ).setChatLineId(MessageID.Help).chat();
                    break;
            }
            break;
        case "s":
        case "sh":
        case "sha":
        case "shar":
        case "share":
            switch (str[1]) {
                case "l":
                case "list":
                    let ListMessage = new Message(
                        "§6§l[BZA] §a稀有掉落消息复制与自动发送列表§7:","\n",
                        new TextComponent(`§1§m                                                                  `).setHover("show_text","§6这是一条分割线") , ' \n' , 
                    ).setChatLineId(MessageID.RareDropSetting);
                    for (let key in RareDropItems.Share) {
                        ListMessage.addTextComponent("§b" + key + " ".repeat(30-key.length) + "§7- ");
                        ListMessage.addTextComponent(new TextComponent((RareDropItems.Share[key].copy ? "§a" : "§e") + "复制").setHover("show_text","§e点击开关 复制 " + key + " 的掉落信息").setClick("run_command","/babyzombieaddons raredrop share set copy " + key));
                        ListMessage.addTextComponent("   ");
                        ListMessage.addTextComponent(new TextComponent((RareDropItems.Share[key].ac ? "§a" : "§e") + "公屏").setHover("show_text","§e点击开关 自动发送 " + key + " 的掉落信息到公屏").setClick("run_command","/babyzombieaddons raredrop share set ac " + key));
                        ListMessage.addTextComponent("   ");
                        ListMessage.addTextComponent(new TextComponent((RareDropItems.Share[key].pc ? "§a" : "§e") + "小队").setHover("show_text","§e点击开关 自动发送 " + key + " 的掉落信息到队伍聊天").setClick("run_command","/babyzombieaddons raredrop share set pc " + key));
                        ListMessage.addTextComponent("   ");
                        ListMessage.addTextComponent(new TextComponent((RareDropItems.Share[key].gc ? "§a" : "§e") + "公会").setHover("show_text","§e点击开关 自动发送 " + key + " 的掉落信息到公会频道").setClick("run_command","/babyzombieaddons raredrop share set gc " + key));
                        ListMessage.addTextComponent("   ");
                        ListMessage.addTextComponent(new TextComponent("§c删除").setHover("show_text","§e点击将 " + key + " 从稀有掉落屏蔽列表中移除").setClick("run_command","/babyzombieaddons raredrop share listremove " + key));
                        ListMessage.addTextComponent("\n");
                    }
                    ListMessage.addTextComponent(new TextComponent(`§1§m                                                                  `).setHover("show_text","§6这是一条分割线"));
                    ListMessage.chat();
                    break;
                case "a":
                case "add":
                    const itemname = str.slice(2).join(' ').toLowerCase()
                    RareDropItems.Share[itemname] = { copy : false, ac : false, pc : false, gc : false };
                    new Message(
                        "§6§l[BZA] §a已将 §b" + str.slice(2).join(' ').toLowerCase() + " §a添加至稀有掉落消息复制与自动发送列表, ",
                        new TextComponent("§d§l前往设置").setHover("show_text","§e你需要在这里打开或关闭消息的复制和自动发送").setClick("run_command","/babyzombieaddons raredrop share list")
                        ).chat();
                    SaveFile();
                    break;
                case "d":
                case "del":
                case "delete":
                case "r":
                case "rem":
                case "remove":
                    if(RareDropItems.Share[str.slice(2).join(' ')] != undefined) {delete RareDropItems.Share[str.slice(2).join(' ')];ChatLib.chat("§6§l[BZA] §a已从稀有掉落消息复制与自动发送列表中删除§7: §b" + str.slice(2).join(' ').toLowerCase());}
                    else ChatLib.chat("§6§l[BZA] §e稀有掉落消息复制与自动发送列表中未找到§7: §b" + str.slice(2).join(' ').toLowerCase());
                    SaveFile();
                    break;
                case "listremove":
                    if(RareDropItems.Share[str.slice(2).join(' ')] != undefined) delete RareDropItems.Share[str.slice(2).join(' ')];
                    SaveFile();
                    ChatLib.command("babyzombieaddons raredrop share list",true);
                    break;
                case "s":
                case "set":
                    const item = str.slice(3).join(' ').toLowerCase(); if(!item) return;
                    if(["copy","ac","pc","gc"].indexOf(str[2]) == -1) {ChatLib.chat("§6§l[BZA] §e在e稀有掉落消息复制与自动发送中未找到叫 " + str[2] + " 的设置");return;}
                    if(RareDropItems.Share[item]) RareDropItems.Share[item][str[2]] = !RareDropItems.Share[item][str[2]];
                    SaveFile();
                    ChatLib.command("babyzombieaddons raredrop share list",true);
                    break;
                default:
                    new Message(
                        new TextComponent("§4←§r").setClick("run_command",`/${settings.modulecmd} raredrop`).setHover("show_text","§b返回上一页"),
                        new TextComponent("                       §b稀有掉落复制和自动发送 功能帮助\n"),
                        new TextComponent("§1§m                                                                  ").setHover("show_text","§6这是一条分割线") , ' \n' , 
                        new TextComponent(`§a/${settings.modulecmd} raredrop share add <物品名> §7- §a添加物品`).setClick("suggest_command",`/${settings.modulecmd} raredrop share add `).setHover("show_text","§a物品名不区分大小写\n§e点击快速输入指令"),"\n",
                        new TextComponent(`§a/${settings.modulecmd} raredrop share del <物品名> §7- §a移除物品`).setClick("suggest_command",`/${settings.modulecmd} raredrop share del `).setHover("show_text","§a物品名不区分大小写\n§e点击快速输入指令"),"\n",
                        new TextComponent(`§a/${settings.modulecmd} raredrop share list        §7- §a查看列表`).setClick("run_command",`/${settings.modulecmd} raredrop share list`).setHover("show_text","§e查看所有已添加的物品并进行管理"),"\n",
                        new TextComponent("§a可检测怪物的稀有掉落,slayer boss的掉落,农业套装效果,花园虫子掉落"),"\n",
                        new TextComponent("§1§m                                                                  ").setHover("show_text","§6这是一条分割线")
                    ).setChatLineId(MessageID.Help).chat();
                    break;
            }
            break;
        default:
            new Message(
                new TextComponent("§4←§r").setClick("run_command",`/${settings.modulecmd} help`).setHover("show_text","§b返回上一页"),
                new TextComponent("                       §b稀有掉落 功能帮助\n"),
                new TextComponent("§1§m                                                                  ").setHover("show_text","§6这是一条分割线") , ' \n' , 
                new TextComponent(`§a/${settings.modulecmd} raredrop ignore §7- §a屏蔽稀有掉落提示`).setClick("run_command",`/${settings.modulecmd} raredrop ignore help`).setHover("show_text","§e点击查看功能帮助"),"\n",
                new TextComponent(`§a/${settings.modulecmd} raredrop share  §7- §a稀有掉落复制和自动发送`).setClick("run_command",`/${settings.modulecmd} raredrop share help`).setHover("show_text","§e点击查看功能帮助"),"\n",
                new TextComponent("§1§m                                                                  ").setHover("show_text","§6这是一条分割线")
            ).setChatLineId(MessageID.Help).chat();
            break;
    }
}

let IgnoreRareDropSound = 0;
const RareDropReg = /(PET|RARE|VERY RARE|CRAZY RARE|INSANE) (DROP|CROP)! /;
const MagicFindReg = / \((.+(✯.+|☘)|Armor Set Bonus)\)/;

register("chat", (message,event) => {
    if(!location.inSkyblock) return;
    if(message.search(RareDropReg) != 0) return;
    let ItemName = message.replace(RareDropReg,"").replace(MagicFindReg,"");
    if(ItemName[0] == "(") ItemName = ItemName.slice(1,-1);
    if(ItemName.search(/[0-9]+x /) == 0) ItemName = ItemName.replace(/[0-9]+x /,"");
    if(RareDropItems.Ignore.indexOf(ItemName.toLowerCase()) != -1) {
        cancel(event);
        IgnoreRareDropSound = ServerTick.getTime();
    }
    if(RareDropItems.Share[ItemName.toLowerCase()] != undefined) {
        if(RareDropItems.Share[ItemName.toLowerCase()].copy) copyToClipboard(message,"§6§l[BZA] §a已复制 §b" + ItemName + " §r§a的掉落消息到剪贴板");
        if(RareDropItems.Share[ItemName.toLowerCase()].ac) ChatLib.command("ac " + message);
        if(RareDropItems.Share[ItemName.toLowerCase()].pc) ChatLib.command("pc " + message);
        if(RareDropItems.Share[ItemName.toLowerCase()].gc) ChatLib.command("gc " + message);
    }
}).setCriteria("${message}")

register("soundPlay",(pos,name,vol,pit,cate,event) => {
    if(!location.inSkyblock || IgnoreRareDropSound == 0) return;
    if(ServerTick.getTime() - IgnoreRareDropSound > 1000) {IgnoreRareDropSound = 0;return;}
    if(name == "note.pling" || name == "random.orb") cancel(event);
})